package com.example.iotest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
import okio.Sink;
import okio.Source;

public class Main {
    public static void main(String[] args) {
        //        javaIOWrite();
        //        javaIORead();
        //        javaIOCopy();
        //        okIORead();
        okIOCopy();
    }

    private static void javaIOWrite() {
        try(BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("./iotest/text.txt")))) {
            writer.write("aa");
            writer.write("bb\n");
            writer.write("cc\n");
            writer.write("dd\n");
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }

    private static void javaIORead() {
        try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("./iotest/text.txt")))) {
            String msg;
            while((msg = reader.readLine()) != null) {
                System.out.println(msg);
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }

    private static void javaIOCopy() {
        try(FileInputStream fileInputStream = new FileInputStream("./iotest/text.txt");
            FileOutputStream fileOutputStream = new FileOutputStream("./iotest/text_java_copy.txt")) {
            byte[] bytes = new byte[1024];
            int len;
            while((len = fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, len);
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }

    private static void okIORead() {
        try(Source source = Okio.source(new File("./iotest/text.txt"))) {
            Buffer buffer = new Buffer();
            source.read(buffer, 1024);
            String msg;
            while((msg = buffer.readUtf8Line()) != null) {
                System.out.println(msg);
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }

    private static void okIOCopy() {
        try(BufferedSource source = Okio.buffer(Okio.source(new File("./iotest/text.txt")));
            BufferedSink sink = Okio.buffer(Okio.sink(new File("./iotest/text_okio_copy.txt")))) {
            byte[] bytes = new byte[1024];
            while((source.read(bytes) != -1)) {
                sink.write(bytes);
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
}